import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.bouncycastle.est.CTEChunkedInputStream;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Set;

public class FilstText {
    public void test_dilireba() throws InterruptedException {
        //驱动程序管理的自动化
        WebDriverManager.chromedriver().setup();

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);
        Thread.sleep(3000);

        //2.输入百度网址:https://www.baidu.com
        driver.get("https://www.baidu.com");

        //3、找到输入框并输入"迪丽热巴"
        //driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("迪丽热巴");
//        driver.findElement(By.xpath("//*[@id=\"chat-textarea\"]")).sendKeys("迪丽热巴");
//        driver.findElement(By.xpath("//*[@id=\"chat-textarea\"]")).clear();
//        driver.findElement(By.xpath("//*[@id=\"chat-textarea\"]")).sendKeys("周杰伦");
//        //4、找到"百度一下"按钮并点击
//        driver.findElement(By.xpath("//*[@id=\"chat-submit-button\"]")).click();

//driver.findElement(By.cssSelector("#chat-submit-button")).click()
        //跳转前的标题和url
        String textBefor = driver.getTitle();
        String urlBefor= driver.getCurrentUrl();
        System.out.println("textBefor:" + textBefor );
        System.out.println("urlBefor:"+urlBefor);
//百度手写点击图片超链接跳转到图片
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();
        //获取当前窗口句柄
        String currentHandle = driver.getWindowHandle();
        //获取所有窗口句柄
        Set<String> allHandles = driver.getWindowHandles();
        //切换到新窗口
        //遍历所有窗口句柄
        for(String handle : allHandles) {
            //判断窗口是否为当前窗口,是则跳过,不是则执行判断条件里面的语句
            if (!handle.equals(currentHandle)) {
                //切换到新窗口
                driver.switchTo().window(handle);
                break;
            }
        }
        //跳转到图片页面,获取跳转后的标题和url
        String textafter = driver.getTitle();
        String urlafter =driver.getCurrentUrl();
        System.out.println("textafter:"+textafter);
        System.out.println("urlafter:"+urlafter);

        //判断页面在哪里
        // driver.findElement(By.cssSelector("#chat-textarea"));
        //查找跳转后页面的查找元素
        //driver.findElement(By.cssSelector("#app > div > div.scroll-container_3GMO1.cos-flex.cos-flex-1.scroll-wrapper_BN2t5 > div > div.cos-flex.cos-flex-col.index-wrapper_B1TLY > div.tools-wrap_1mLZ7 > div._list-wrap_11emz_1._listWrap_186pi_2 > div._list_11emz_1 > div > div:nth-child(1) > a > img"));

        Thread.sleep(3000);

        //5、关闭浏览器
        driver.quit();
    }
    public void test01() throws InterruptedException, IOException {
        //驱动程序管理的自动化
        WebDriverManager.chromedriver().setup();

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.baidu.com");
      //  Thread.sleep(3000);
        //

        getScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName(),driver);

          driver.quit();
//        driver.manage().window().maximize();
//        Thread.sleep(3000);
//
//        driver.manage().window().minimize();
//        Thread.sleep(3000);
//
//        driver.manage().window().fullscreen();
//        Thread.sleep(3000);
//
//        driver.manage().window().setSize(new Dimension(100, 100));
//        Thread.sleep(3000);

//        driver.close();
    }
    public void getScreenShot(String str,WebDriver driver) throws IOException {
        SimpleDateFormat sim1=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sim2=new SimpleDateFormat("hh-mm-ss");
        String dirTime = sim1.format(System.currentTimeMillis());
        String filetime = sim2.format(System.currentTimeMillis());

        String filename = "./src/test/images"+dirTime+"/"+str+"-"+filetime+".png";
        File srcfile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(srcfile,new File(filename));
    }
    public void test02() throws InterruptedException, IOException {
        //驱动程序管理的自动化
        WebDriverManager.chromedriver().setup();

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.baidu.com");
        //隐式等待
       // driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        //显示等待
        WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("#chat-textarea")));

        driver.quit();
    }

    public void test03() throws InterruptedException, IOException {
        //驱动程序管理的自动化
        WebDriverManager.chromedriver().setup();

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);
        driver.navigate().to("https://tool.lu/");

        driver.findElement(By.cssSelector("#nav > div > div > ul > li:nth-child(6) > a")).click();
        Thread.sleep(3000);
        driver.navigate().back();//返回上一页   后退
        Thread.sleep(3000);
        driver.navigate().forward();//前进
        Thread.sleep(3000);
        driver.navigate().refresh();//刷新页面(最后到达软件页面)
        Thread.sleep(3000);
        driver.quit();

    }
    public void test04() throws InterruptedException, IOException {
        //驱动程序管理的自动化
        WebDriverManager.chromedriver().setup();

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);
        driver.get("file:///D:/%E5%BE%AE%E4%BF%A1%E4%B8%8B%E8%BD%BD/selenium-html/Prompt.html");
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("body > input[type=button]")).click();
        Thread.sleep(3000);
        //调用弹窗
        Alert alert = driver.switchTo().alert();

        //先输入文本然后点击取消
        alert.sendKeys("你好");
        Thread.sleep(3000);
       // alert.dismiss();
        //点击确定
        alert.accept();

        Thread.sleep(3000);

        driver.quit();
    }
    //文件上传
    public void test05() throws InterruptedException, IOException {
        //驱动程序管理的自动化
        WebDriverManager.chromedriver().setup();

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
        options.addArguments("--remote-allow-origins=*");

        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);
        driver.get("file:///D:/%E5%BE%AE%E4%BF%A1%E4%B8%8B%E8%BD%BD/selenium-html/upload.html");
        Thread.sleep(3000);
        //对按钮输入文本
        driver.findElement(By.cssSelector("body > div > div > input[type=file]")).sendKeys("D:\\java116\\java-test\\test.txt");
        Thread.sleep(3000);
        driver.quit();
    }
    public void test06() throws InterruptedException, IOException {
        //驱动程序管理的自动化
        WebDriverManager.chromedriver().setup();

        ChromeOptions options = new ChromeOptions();
        //允许访问所有链接
         options.addArguments("--remote-allow-origins=*");
        //增加配置--无头模式
        options.addArguments("--headless");


        //1、打开浏览器
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.baidu.com");

        //对按钮输入文本
        driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("迪丽热巴");
        driver.findElement(By.cssSelector("#chat-submit-button")).click();
        driver.quit();
    }

}
